#!/data/Software/mydan/perl/bin/perl
use strict;
use warnings;
use FindBin qw( $RealBin );
use YAML::XS;

=head1 SYNOPSIS

 $0

=cut

print "stop agent\n";

my $Config = eval{ YAML::XS::LoadFile "$RealBin/../config/Config";};
die "load Config fail: $!" unless $Config;

my $envname = `cat $RealBin/../conf/envname`;
chomp $envname;
die "envname undef" unless $envname;

die "envname $envname undef in Config" unless $Config = $Config->{$envname};

die "port undef" unless my $port = $Config->{port};


system "killall agent_supervisor 2>/dev/null";

for my $type ( qw( api api.slave ) )
{
    map{
        system '/usr/sbin/lsof -i:'.$_.'|tail -n 1|awk \'{print $2}\'|xargs -i{} kill {}';
        print "stop $type port: $_\n";
    }$port->{$type}{from} .. $port->{$type}{to};
}

map{
    system "killall agent_server_$_";
    print "stop agent_server_$_\n"
}qw( call bury keepalive check inherit );
